<template>
  <div class="search" :style="{ '--themeColor': themeColor }">
    <nav-bar title="附近商圈" />
    <div class="head">
      <div class="input">
        <u-search
          shape="round"
          :focus="isFocus"
          bgColor="#F7F7F7"
          clearabled
          v-model="queryParams.shopName"
          searchIconSize="26"
          maxlength="10"
          @search="fetData"
          @custom="fetData"
          height="62rpx"
          placeholder="请输入搜索内容"
        />
      </div>
    </div>
    <div class="shop-list">
      <mescroll-uni
        :fixed="false"
        ref="mescrollRef"
        @down="downCallback"
        @up="upCallback"
        :up="{
          auto: false,
          textNoMore: '没有更多了',
          empty: {
            tip: '您的附近暂时没有商圈',
            icon: 'https://hyyxboss.oss-cn-shenzhen.aliyuncs.com/img/pub/empty-nearby-business-district.png',
            fixed: true,
            top: '40%',
          },
        }"
        :down="{
          auto: false,
        }"
      >
        <div
          v-for="(item, index) in dataList"
          :key="index"
          class="shop-content"
        >
          <div class="img">
            <xh-img
              borderRadius="10rpx"
              :src="item.logo"
              @click="
                $myTool.jumpPage('businessDistrictIndexPage', {
                  shopId: item.id,
                })
              "
            />
          </div>
          <div class="msg">
            <div
              class="top"
              @click="
                $myTool.jumpPage('businessDistrictIndexPage', {
                  shopId: item.id,
                })
              "
            >
              <div class="title">
                <view class="shopName">{{ item.name }}</view>
                <div class="distance" v-if="item.distance">
                  {{ $myTool.transitionDistance(item.distance) }}
                </div>
              </div>
              <div class="detail">
                <div class="left">
                  <!--                  <div :class="['falg', item.isOpen ? 'open' : '']">-->
                  <!--                    {{ item.isOpen ? "营业中" : "休息中" }}-->
                  <!--                  </div>-->
                  <div class="type">商家{{ item.shopNumbers || 0 }}</div>
                  <div class="month">月售{{ item.monthSales }}</div>
                </div>
              </div>
            </div>

            <div class="store-val coupons" v-if="item.pubCoupons">
              <view class="ticket">
                <scroll-view class="ticket_list" scroll-x="true">
                  <ticket-item
                    v-for="coupon in item.pubCoupons"
                    :key="coupon.id"
                    :denomination="coupon.denomination"
                    :discountPercent="coupon.discountPercent"
                    :transactionMinimum="coupon.transactionMinimum"
                    :receiveCode="coupon.receiveCode"
                  />
                </scroll-view>
                <view
                  class="see_all"
                  v-if="item.pubCoupons && item.pubCoupons.length >= 2"
                  @click="
                    $myTool.jumpPage('businessDistrictIndexPage', {
                      shopId: item.id,
                      showTicket: true,
                    })
                  "
                >
                  查看更多
                </view>
              </view>
            </div>
          </div>
        </div>
      </mescroll-uni>
    </div>
  </div>
</template>

<script>
import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
import { getShops } from "@/service/api/sp-store/index";
import { mapState } from "vuex";
import { receiveCoupon } from "@/service/api/sp-coupon";
import ticketItem from "../../components/ticket-item";
export default {
  mixins: [MescrollMixin], // 使用mixin
  data() {
    return {
      isFocus: false,
      dataList: [],
      queryParams: {
        latitude: "",
        longitude: "",
        shopName: "",
        shopType: 3,
        sortType: 1, // 排序规则 1综合排序 2距离优先 3人气优先
        pageNum: 1,
        pageSize: 10,
        pubTag: 1,
      },
      filterItems: [
        [{}],
        [
          {
            label: "综合排序",
            value: "1",
          },
          {
            label: "距离优先",
            value: "2",
          },
          {
            label: "人气优先",
            value: "3",
          },
        ],
      ],
      defaultList: [],
    };
  },
  components: { ticketItem },
  computed: {
    ...mapState("sp-common/base", ["themeColor"]),
    ...mapState("sp-store/index", ["industryType"]),
  },
  watch: {
    industryType: {
      handler(value) {
        let selectArr = [];
        Object.keys(value).forEach((item) => {
          let obj = {
            label: value[item] || 0,
            value: item,
          };
          selectArr.push(obj);
        });
        this.filterItems[0] = selectArr;
      },
      deep: true,
    },
  },
  onReady() {
    if (this?.$Route?.query?.industryType) {
      this.queryParams.shopType = this?.$Route?.query?.industryType;
      this.defaultList = [this?.$Route?.query?.industryType];
      this.isFocus = false;
    }
    this.getUserLocation();
  },
  mounted() {
    this.fetData();
  },
  methods: {
    setSearch(item) {
      this.queryParams.shopName = item;
      this.fetData();
    },
    changeSelect(val) {
      this.queryParams.shopType = val.key1;
      this.queryParams.sortType = val.key2;
      this.mescroll?.resetUpScroll();
    },
    fetData() {
      let {
        queryParams,
        queryParams: { pageNum },
      } = this;
      getShops(queryParams)
        .then((res) => {
          let { total, list = [] } = res;
          if (!list) list = [];
          list.map((item) => {
            if (item.pubCoupons) {
              item.pubCoupons.map((pubCoupon) => {
                pubCoupon.pullDown = false;
              });
            }
          });
          if (pageNum === 1) this.dataList = [];
          this.mescroll.endBySize(list.length || 0, total);
          this.dataList =
            pageNum === 1 ? list || [] : this.dataList.concat(list);
        })
        .catch(() => {
          this.mescroll?.endErr();
        });
    },
    downCallback(page) {
      this.queryParams.pageNum = 1;
      this.mescroll.resetUpScroll();
    },
    upCallback(page) {
      this.queryParams.pageNum = page.num;
      this.fetData();
    },
    getUserLocation() {
      let {
        queryParams: { longitude, latitude },
      } = this;
      if (!longitude || !latitude) {
        this.$getUserLocation()
          .then((res) => {
            let result = res?.currentLocation
              ? res?.currentLocation
              : res?.location;
            let { longitude, latitude } = result;
            this.queryParams = Object.assign({}, this.queryParams, {
              longitude,
              latitude,
            });
            if (this.queryParams.shopType || this.defaultList.length > 0) {
              this.mescroll.resetUpScroll();
            }
          })
          .catch(() => {
            this.mescroll.endErr();
          });
        return;
      }
    },
    // 领取优惠券
    pullDownCoupon(coupon, parentIndex, couponsIndex) {
      let { receiveCode } = coupon;
      let { dataList } = this;
      if (!dataList[parentIndex]?.pubCoupons) return;
      if (dataList[parentIndex].pubCoupons[couponsIndex].pullDown) return;
      receiveCoupon({
        receiveCode,
      })
        .then(() => {
          this.dataList[parentIndex].pubCoupons[couponsIndex].pullDown = true;
          uni.showToast({
            title: "领取成功，赶快去下单吧",
          });
        })
        .catch((err) => {
          uni.showToast({
            title: err?.msg,
            icon: "none",
          });
        });
    },
  },
};
</script>

<style lang="scss" scoped>
page {
  width: 100%;
  padding-bottom: 120rpx;
  padding-top: 65px;
  background-color: #f7f7f7;
}

.search {
  //   width: 100%;
  display: flex;
  flex-direction: column;
  height: 100vh;
  //background-color: #f7f7f7;
  background-color: #fff;
  .head {
    background: #fff;
    width: 100%;
    padding-top: 10rpx;

    .input {
      display: flex;
      align-items: center;
      // padding: 0 30rpx;
      margin: 0 auto;
      margin-bottom: 10rpx;
      margin-top: 8rpx;
      width: calc(100% - 60rpx);
      box-shadow: 0px 0px 4rpx 0px rgba(34, 34, 34, 0.14);
      border-radius: 40rpx;
      font-size: 29rpx;

      ::v-deep .u-search {
        padding: 0 30rpx;
        background-color: #f7f7f7;
        .u-search__content {
          padding: 0 !important;
        }
        ::v-deep .u-search__content__close {
          width: 30rpx;
          height: 30rpx;
          transform: scale(1) !important;
          -webkit-transform: scale(1) !important;
        }
        .u-icon__icon {
          // color: #888888 !important;
        }
        .u-search__action {
          font-size: 29rpx;
          font-weight: 500;
          padding-left: 50rpx;
          position: relative;
          &::after {
            width: 1px;
            height: 35rpx;
            background: #dddddd;
            content: "";
            position: absolute;
            left: 24rpx;
            top: 50%;
            transform: translateY(-50%);
          }
        }
      }
    }
  }

  .shop-list {
    flex: 1;
    overflow-y: scroll;
    position: relative;
    width: calc(100%);
    margin: 20rpx auto;
    background-color: #fff;

    .shop-content {
      display: flex;
      padding: 20rpx 30rpx;
      margin: 20rpx auto;
      background-color: #fff;
      border-radius: 18rpx;

      .img {
        width: 170rpx;
        height: 170rpx;
        margin-right: 20rpx;
      }

      .msg {
        flex: 1;
        width: 0;
        display: flex;
        flex-direction: column;

        // justify-content: space-between;
        .title {
          color: #181818;
          margin-bottom: 20rpx;
          display: flex;
          align-items: baseline;
          justify-content: space-between;
        }
        .distance {
          font-size: 22rpx;
          font-family: PingFang SC;
          font-weight: 500;
          color: #999999;
        }
        .shopName {
          font-weight: bold;
          font-size: 32rpx;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
        }

        .detail {
          display: flex;
          align-items: center;
          font-size: 27rpx;
          font-weight: 500;
          color: #888888;
          justify-content: space-between;

          .left {
            display: flex;
            align-items: center;

            .falg {
              width: 80rpx;
              height: 32rpx;
              text-align: center;
              line-height: 30rpx;
              background: #f7f7f7;
              border: 1rpx solid #999999;
              font-size: 22rpx;
              font-weight: 500;
              border-radius: 5rpx;
              margin-right: 10rpx;
            }

            .open {
              background: #e2f8f0;
              border: 1rpx solid #0cd38a;
              color: #0cd38b !important;
            }

            .month {
              font-size: 24rpx;
              font-family: PingFang SC;
              font-weight: 500;
              color: #999999;
            }
            .type {
              @extend .month;
              padding-right: 15rpx;
              margin-right: 15rpx;
              position: relative;
              &::after {
                content: "";
                width: 1px;
                height: 27rpx;
                background-color: #ddd;
                position: absolute;
                right: 0;
                top: 50%;
                transform: translateY(-50%);
              }
            }
          }
        }

        .store-val {
          font-size: 29rpx;
          font-weight: 500;
          // color: #fff;
          margin-top: auto;
          .ticket {
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 100%;
            //margin: 0 auto;
            margin-top: 24rpx;
            font-family: PingFang SC;
            .ticket_list {
              flex: 1;
              overflow: hidden;
              white-space: nowrap;
              .ticket_item {
                font-size: 22rpx;
                font-weight: 500;
                color: #ffffff;
                position: relative;
                z-index: 5;
                display: inline-block;
                align-items: center;
                height: 50rpx;
                background: linear-gradient(-90deg, #daa629, #ebbc64);
                padding-left: 15rpx;
                border-radius: 10rpx;
                overflow: hidden;
                margin-right: 20rpx;
                .discount {
                  display: inline-block;
                  font-size: 22rpx;
                  font-weight: bold;
                  color: #ffffff;
                  margin-right: 15rpx;
                }
                .rule {
                  display: inline-block;
                  position: relative;
                  margin-right: 15rpx;

                  .round_top {
                    position: absolute;
                    height: 8rpx;
                    width: 8rpx;
                    background-color: #ffffff;
                    top: -18rpx;
                    right: -18rpx;
                    border-radius: 50%;
                  }
                  .round_bottom {
                    position: absolute;
                    height: 8rpx;
                    width: 8rpx;
                    background-color: #ffffff;
                    bottom: -16rpx;
                    right: -18rpx;
                    border-radius: 50%;
                  }
                }

                .get {
                  &.took {
                    background: #e9d7aa;
                  }

                  display: inline-block;
                  line-height: 50rpx;
                  padding: 0 15rpx;
                }
              }
            }

            .see_all {
              white-space: nowrap;
              font-size: 26rpx;
              font-family: PingFang SC;
              font-weight: 500;
              color: var(--theme-color);
            }
          }
        }
      }
    }
  }

  .histoy {
    margin-top: 15rpx;
    padding: 0 30rpx;
    border-top: 1px solid #ececec;
    height: 100%;
    background-color: #fff;
    .top {
      display: flex;
      justify-content: space-between;
      margin-top: 35rpx;
      align-items: center;
      margin-bottom: 30rpx;
    }

    .list {
      display: flex;
      flex-wrap: wrap;

      .one {
        height: 52rpx;
        background: #f5f5f5;
        border-radius: 26rpx;
        padding: 0 20rpx;
        line-height: 52rpx;
        font-size: 28rpx;
        font-weight: 500;
        color: #888888;
        margin-right: 20rpx;
        margin-bottom: 20rpx;
      }
    }
  }
}
</style>
